// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_plan_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type BasicPlanItemInfoDbItem struct {
	Title     string `bson:"title"`
	Content   string `bson:"content"`
	StartTime int64  `bson:"startTime"`
	EndTime   int64  `bson:"endTime"`
}

type PlanItemInfoDbItem struct {
	ItemId     string                  `bson:"_id"`
	RoadmapId  string                  `bson:"roadmapId"`
	PlanId     string                  `bson:"planId"`
	BasicInfo  BasicPlanItemInfoDbItem `bson:"basicInfo"`
	CreateTime int64                   `bson:"createTime"`
	UpdateTime int64                   `bson:"updateTime"`
}

func (item *PlanItemInfoDbItem) ToPlanItemInfo() *roadmap_plan_api.PlanItemInfo {
	return &roadmap_plan_api.PlanItemInfo{
		ItemId: item.ItemId,
		PlanId: item.PlanId,
		BasicInfo: &roadmap_plan_api.BasicPlanItemInfo{
			Title:     item.BasicInfo.Title,
			Content:   item.BasicInfo.Content,
			StartTime: item.BasicInfo.StartTime,
			EndTime:   item.BasicInfo.EndTime,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _PlanItemInfoDao struct {
	mc *mongo.Collection
}

func newPlanItemInfoDao(db *mongo.Database) (*_PlanItemInfoDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "plan_item")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"roadmapId", 1}, {"planId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_PlanItemInfoDao{mc: mc}, nil
}

func (db *_PlanItemInfoDao) Insert(ctx context.Context, dbItem *PlanItemInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_PlanItemInfoDao) Exist(ctx context.Context, roadmapId, planId, itemId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"roadmapId": roadmapId, "planId": planId, "_id": itemId})
	return count > 0, err
}

func (db *_PlanItemInfoDao) Get(ctx context.Context, roadmapId, planId, itemId string) (*PlanItemInfoDbItem, error) {
	retItem := &PlanItemInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"roadmapId": roadmapId, "planId": planId, "_id": itemId}).Decode(retItem)
	return retItem, err
}

func (db *_PlanItemInfoDao) Remove(ctx context.Context, roadmapId, planId, itemId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"roadmapId": roadmapId, "planId": planId, "_id": itemId})
	return err
}

func (db *_PlanItemInfoDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_PlanItemInfoDao) List(ctx context.Context, roadmapId, planId string) ([]*PlanItemInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId, "planId": planId}, options.Find().SetSort(bson.M{"basicInfo.startTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*PlanItemInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_PlanItemInfoDao) UpdateBasic(ctx context.Context, roadmapId, planId, itemId string, basicItem *BasicPlanItemInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"roadmapId": roadmapId, "planId": planId, "_id": itemId},
		bson.M{
			"$set": bson.M{
				"basicInfo":  basicItem,
				"updateTime": updateTime,
			},
		})
	return err
}
